import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class ztest3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1T恤，2衬衫， 3蕾丝衫/雪纺衫，4连衣裙， 5大码连衣裙，6牛仔裤，7休闲裤 "20000035","20000018",
		String vicds[]  = {"20000106"};
		
		for (int i = 1; i < 100; i++) {
			for (String vicd : vicds) {
				try {
					CrawlPages(i,vicd);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	

	private static void CrawlPages(int page,String vicds) throws Exception{
		
	     String buf =null;
         String newbuf =null;
         //merge=1合并想同款pid=1女装searchCity=gz广州currentPage=1第一页开始
         //T恤vcid=20000035
         
         String zqUrl = "https://www.vvic.com/apic/search/asy?merge=1&pid=1&vcid="+vicds+"&searchCity=gz&currentPage="+page;
         
         String vcid = Util.getProductCategories(zqUrl);
         
         buf= pickData(zqUrl);
         
	     JSONObject jsonObject = JSONObject.parseObject(buf.toString().trim());
	     String data = jsonObject.getString("data");
	     
	     JSONObject jsonObjectdata = JSONObject.parseObject(data);
	     String search_page = jsonObjectdata.getString("search_page");
	     
	     JSONObject search_pageobj = JSONObject.parseObject(search_page);
	     String recordList = search_pageobj.getString("recordList");
	    
		  
	     JSONArray jsonArray = JSONArray.parseArray(recordList);
	     System.out.println(jsonArray.size());
	   
	     	 int i=0;
	     	 int b=1;
		     for (Object  object : jsonArray) {
		    	 
		    	 	JSONObject job  = jsonArray.getJSONObject(i);
		    	 	String itemId= job.getString("item_id");
		    	 
			    	if (itemId != null &&itemId.length() != 0) {
			    		 System.out.println("id为:"+itemId);
			    		 System.out.println("档口为:"+job.getString("merchant_coding"));
			    		 System.out.println("货号为:"+job.getString("art_no"));
			    		 System.out.println("上新时间为:"+job.getString("up_time2"));
			    		 System.out.println("颜色时间为:"+job.getString("color"));
			    		 System.out.println("尺寸时间为:"+job.getString("size"));
			    		 System.out.println("代发时间为:"+job.getString("sales"));
			    		 System.out.println("标题时间为:"+job.getString("title"));
			    		 System.out.println("批发价格为:"+job.getString("discount_price"));
			    		 System.out.println("淘宝价格为:"+job.getString("price"));
			    		 System.out.println("https://www.vvic.com/item/"+itemId);
			     
			    		 Thread.sleep(1500);
			    		 
			    		 newbuf = pickData("https://www.vvic.com/item/"+itemId);
			    		 Document document = Jsoup.parse(newbuf);
			    		 
			    		 Product product = new Product();
			    		 
			    		 product.setColour(job.getString("color"));
			    		 product.setSize(job.getString("size"));
			    		 product.setSupplierProductid(itemId);//搜款网的商品ID
			    		 product.setProductCode(RandomNumberGenerator.generateNumber());//随机生成码
//			    		 product.setSupplierHuohao(job.getString("art_no"));//供应商货号
			    		 product.setProductName(job.getString("title"));//标题
			    		 product.setProductCost(Float.parseFloat(job.getString("discount_price")));//商品成本采购价
			    		 product.setProductCostSuggest(Float.parseFloat(job.getString("price")));//淘宝价格
			    		 
			    		 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
			    		 String c=sdf.format(new Date());
			    		 int date = Integer.parseInt(c);
			    		 product.setTimeAdd(date);
			    		 product.setCategoryId(Integer.parseInt(vcid));//产品分类
			    		 
			    		 getSuggestion(document,product,newbuf);
			    		 System.out.println("--------------------------------------分类"+vicds+"第"+page+"页"+ b++ +"条数据--------------------------------------------");
			    		 
					}
	    	 i++;
		}
		
		
	}

	
	  private static String getSuggestion(Document doc,Product product,String html){
	        Element element=doc.getElementById("thumblist");
	        StringBuffer sb=new StringBuffer();

	        if (element!=null) {
	        	
      	   Element element1 = doc.getElementById("descTemplate");
      	   
      	   product.setTextDescription(element1.toString());//详情html
      	   
      	   String detailsHtml = Util.getdetailsHtml(element1.toString());
      	   Document detailsHtmlDoc = Jsoup.parse(detailsHtml);
      	   Elements elementDetails = detailsHtmlDoc.getElementsByTag("img");
      	   StringBuffer detailsHtmlimgs = new StringBuffer();
      	   for (Element element2 : elementDetails) {
      		   detailsHtmlimgs.append(element2.toString());
			   }
      	   
  	       System.out.println("详情html<script>:"+element1.toString());
  	       System.out.println("详情htmlimg:"+detailsHtmlimgs);
  	       
  	       
  	        product.setTextImgDescription(detailsHtmlimgs+"");
	   	       	Elements element11 = doc.select("div.shop-content"); 
	   	       	Elements spanName = element11.select("span");
	   	       	System.out.println("档口名称："+spanName.get(0).text());	
	   	        product.setSupplierName(spanName.get(0).text());
  	       
  	       
  	       	Elements element2 = doc.getElementsByClass("mt10");
  	       	Elements elements3 = element2.select("p");
  	       	StringBuffer phone= new StringBuffer();
  	       	int i = 1;
  	       	for (Element element3 : elements3) {
  	       		System.out.println("电话为："+element3.text());
  	       		if (i<elements3.size()) {
  	       			phone.append(element3.text()+"&");
					}else{
						phone.append(element3.text());
					}
  	       		
  	       		i++;
				}
  	       	product.setSupplierPhone(phone.toString());
  	       	Elements elements4 =  element2.select("li");
  	       	
  	       	for (Element element3 : elements4) {
  	       		Elements elements7 = element3.select("div");
  	       		if(("QQ：").equals(elements7.get(0).text())){
  	    	       	System.out.println("QQ："+elements7.get(1).text());
  	    	       	product.setSupplierqq(elements7.get(1).text());
  	       		}
  	       		
  	       		if(("微信：").equals(elements7.get(0).text())){
  	    	       	System.out.println("微信："+elements7.get(1).text());
  	    	       	product.setSupplierWeixin(elements7.get(1).text());
  	       		}
  	       		
  	       		if(("旺旺：").equals(elements7.get(0).text())){
  	       			Elements elements8 = elements7.select("span");
  	    	       	System.out.println("旺旺："+elements8.get(0).text());
  	    	       	product.setSupplierWangwang(elements7.get(1).text());
  	       		}
  	       		
  	       		
				}
  	       	Element elements5=elements4.get(elements4.size()-1);
  	       	Elements elements6 = elements5.select("div");
  	       	System.out.println("档口地址："+elements6.get(1).text());
  	       	
  	       	product.setSupplierAddress(elements6.get(1).text());
  	       	
  	       	Elements huohao = doc.select("div.value.ff-arial");
  	       	System.out.println("货号名称："	+huohao.get(0).text());
  	       	
  	     	product.setSupplierHuohao(huohao.get(0).text());
  	     	
  	       	Elements element9 = doc.getElementsByClass("d-name");
  	    	Elements element10 = element9.select("a");
  	    	if (element10!=null&&element10.size()>0) {
      	    	System.out.println("淘宝链接："+element10.get(0).attr("href"));		
				}
      	   
	  	      	Elements elements=element.getElementsByTag("img");
      	   
	  	      	List<ImaUrl> list  =new ArrayList();
	  	        for (Element e : elements) {
	  	        	
	  	        	String mid=e.attr("mid");
	  	        	System.out.println("左边商品展示大图："+mid.substring(2, mid.length()));
	  	        	String src=e.attr("src");
	  	        	System.out.println("左边商品展示小图："+src.substring(2, src.length()));
	  	        	String big=e.attr("big");
	  	        	System.out.println("左边商品展示最大图："+big.substring(2, big.length()));
	  	        	
	  	        	ImaUrl  imaUrl = new ImaUrl();
	  	        	imaUrl.setImgUrl(mid);
	  	        	imaUrl.setImgUrlBig(big);
	  	        	imaUrl.setImgUrlSmall(src);
	  	        	list.add(imaUrl);
	  	        }
	  	      product.setExpandDetailsList(list);
	  	      
	  	      Elements selectColorPic = doc.select("li.selectColorPic.selectColor");
	  	      String _COLORstr	= Util.getColorArray(html);
	  	      String[] colors = _COLORstr.split(",");
	  	      int ii = 0;
	  	      List<ImaColor> clist  =new ArrayList();
	  	      for (Element element3 : selectColorPic) {
	  	    	Elements element3s=element3.getElementsByTag("img");
	  	    	if (element3s!=null && element3s.size()>0) {
		  	    	String mid = element3s.get(0).attr("mid");
		  	    	String src = element3s.get(0).attr("src");
		  	    	System.out.println("sku大图。。。--------"+mid);
		  	    	System.out.println("sku小图。。。--------"+src);
		  	    	
		  	    	ImaColor  imaColor =  new ImaColor();
		  	    	imaColor.setColor(colors[ii]);
		  	    	imaColor.setImageUrl(mid);
		  	    	clist.add(imaColor);
				}
	  	    	ii++;
	  	      }
	  	     product.setImaColorList(clist);
	  	        
	  	      Lianjie  lianjie  =  new Lianjie();
	  	      if (product.getColour()!=null&&!product.getColour().equals("")&&product.getSize()!=null&&!product.getSize().equals("")) {
	  	        lianjie.insert_data(product);
	  	      }
	  	      
			}else{
				
			  System.out.println("-----------------------------------抓取失败----------------------------------------");
			}
	        sb.append("");
	        return sb.toString();
	    }
	
	 /*
   * 爬取网页信息
   */
  private static String pickData(String url) {
      CloseableHttpClient httpclient = HttpClients.createDefault();
      try {
          HttpGet httpGet = new HttpGet(url);
          CloseableHttpResponse response = httpclient.execute(httpGet);
          try {
              // 获取响应实体
              HttpEntity entity = response.getEntity();
              // 打印响应状态
              if (entity != null) {
                  return EntityUtils.toString(entity);
              }
          } finally {
              response.close();
          }
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (ParseException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } 
      finally {
          // 关闭连接,释放资源
          try {
              httpclient.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      return null;
  }
  
}
